package com.xinlianshiye.im.dao.impl;

import com.querydsl.core.types.Projections;
import com.xinlianshiye.im.dao.BaseRepository;
import com.xinlianshiye.im.dao.custom.FriendDaoCustom;
import com.xinlianshiye.im.bean.QFriend;
import com.xinlianshiye.im.bean.QUser;
import com.xinlianshiye.im.bean.User;

import java.util.List;

public class FriendDaoImpl extends BaseRepository implements FriendDaoCustom {

    @Override
    public List<User> listGroupId(Integer group) {
        QFriend friend = QFriend.friend1;
        return queryFactory.select(friend.friend).from(friend).where(friend.userGroup.id.eq(group)).fetch();
    }

    @Override
    public List<User> findFriendByName(Integer userId, String name) {
        QFriend friend = QFriend.friend1;
        return queryFactory
                .select(friend.friend)
                .where(friend.user.id.eq(userId).and(friend.friend.username.contains(name)))
                .fetch();
    }

    @Override
    public List<User> findNotFriend(Integer userId) {
        QUser user = QUser.user;
        QFriend friend = QFriend.friend1;
        List<Integer> list = queryFactory.select(friend.friend.id).from(friend)
                .where(friend.user.id.eq(userId)).fetch();
        list.add(userId);
        // 查询我和我的好友之外的其它用户
        return queryFactory
                .select(Projections.bean(User.class, user.id, user.username))
                .from(user)
                .where(user.id.notIn(list)).fetch();
    }
}
